<?php

class MemberController extends Controller
{
	public function actionIndex()
	{
	   $page = isset($_GET['page']) ? ((int) $_GET['page']) : 1;
      $limit = 10;
      $offset = $page*$limit - $limit;
       
		$use_id = Yii::app()->user->id;
      if(is_null($use_id))
         $this->redirect('/dang-nhap');
      $model = Member::model()->findByPk(Yii::app()->user->id);
      
      //Danh sach cac tin da dang
      $criteria = new CDbCriteria();
      $criteria->offset = $offset;
      $criteria->limit = $limit;
      $criteria->addCondition('post_use_id =' . Yii::app()->user->id);
      $criteria->addCondition('post_active = 1');
      $list_posted = Post::model()->with('district','province')->findAll($criteria);
      $count = Post::model()->count($criteria);
      $pagination = new Zebra_Pagination();
      $pagination->records($count);
      $pagination->records_per_page($limit);
      $this->render('index',array(
         'model' => $model,
         'list_posted' => $list_posted,
         'pagination' => $pagination,
      ));
	}
   
   public function actionEdit(){
      $use_id = Yii::app()->user->id;
      if(is_null($use_id))
         $this->redirect('/dang-nhap');
      $this->layout = 'login';
      $model = Member::model()->findByPk(Yii::app()->user->id);
      //CVarDumper::dump($model->attributes,10,true);die();
      if(isset($_POST['Member'])){
         if($_POST['Member']['Password'] != $_POST['Member']['repeatPassword'])
            $model->addError('repeatPassword','Xác nhận mật khẩu không chính xác');
         else{
            if(md5($_POST['Member']['oldPassword']) != $model->Password){
               $model->addError('Password','Mật khẩu cũ không chính xác');
            }else{
               $model->attributes = $_POST['Member'];
               $model->Password = md5($model->Password);
               $model->IsActive = 1;
               if($model->validate())
               {
                  if($model->save())
                  {
                     Yii::app()->user->setFlash('success','Cập nhật thành công');
                  }   
               }else{
                  CVarDumper::dump($model->errors,10,true);die();
               }   
               
                     
            }
         }
          
      }
      $this->render('edit',array(
         'model' => $model,
      ));
   }

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}